import datetime
import json
import re

from re_common.baselibrary.tools.all_requests.aiohttp_request import AioHttpRequest
from re_common.baselibrary.tools.all_requests.mrequest import MRequest
from re_common.baselibrary.utils.basedict import BaseDicts
from re_common.baselibrary.utils.basetime import BaseTime

from apps.crawler_platform.core_platform.core_g import SQLTable, CoreSqlValue
from apps.crawler_platform.core_platform.g_model import DealModel, CallBackModel, \
    DealInsertModel, PaperDaysModel, DealUpdateModel, PaperPageModel, PaperArticleModel
from settings import get_settings

__all__ = [
            "kejiribao_kejiribaostart_callback",
            "kejiribao_kejiribaodays_callback",
            "kejiribao_kejiribaopage_callback",
            "kejiribao_kejiribaoarticle_callback",
           ]


def kejiribao_kejiribaostart_callback(callmodel: CallBackModel[PaperDaysModel]) -> DealModel:
    result = DealModel()
    para_dicts = callmodel.para_dicts
    data = para_dicts["data"]
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    info_dicts = {"task_name": callmodel.sql_model.task_name,
                  "task_tag": callmodel.sql_model.task_tag,
                  "task_tag_next": task_info.task_tag_next}
    if "1_1" in data:
        d_i_model = DealInsertModel()
        d_i_model.insert_pre = CoreSqlValue.insert_ig_it
        for item in data["1_1"]['info']['children']:
            temp = info_dicts.copy()
            temp["task_tag"] = temp["task_tag_next"]
            del temp["task_tag_next"]
            date = item['date']
            year = date.split('-')[0]
            months = date.split('-')[1]
            days = date.split('-')[2]

            temp["year"] = year
            temp["months"] = months
            temp["days"] = days
            temp["url"] = item['url_part']
            temp["days_json"] = '{}'
            d_i_model.lists.append(temp)
        result.befor_dicts.insert.append(d_i_model)
    return result


def kejiribao_kejiribaodays_callback(callmodel: CallBackModel[PaperDaysModel]) -> DealModel:
    result = DealModel()
    para_dicts = callmodel.para_dicts
    data = para_dicts["data"]
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    info_dicts = {"task_name": callmodel.sql_model.task_name,
                  "task_tag": callmodel.sql_model.task_tag,
                  "task_tag_next": task_info.task_tag_next}
    if "1_1" in data:
        di_model = DealInsertModel()
        di_model.insert_pre = CoreSqlValue.insert_ig_it
        for item in data["1_1"]['info']['children']:
            title = item['title']
            url_part = item['url_part']
            year = callmodel.sql_model.year
            months = callmodel.sql_model.months
            days = callmodel.sql_model.days
            page = re.findall('第(.*?)版', title)[0]

            temp_info = info_dicts.copy()
            task_tag_next = temp_info["task_tag_next"]
            temp_info["task_tag"] = task_tag_next
            del temp_info["task_tag_next"]
            temp_info["rawid_page"] = year + months + days + page
            new_dict = dict()
            new_dict["year"] = year
            new_dict["months"] = months
            new_dict["days"] = days
            new_dict["versions_title"] = f'({page})：{title.split("：")[-1]}'
            new_dict["page"] = page
            new_dict["versions_url"] = re.findall("node_.*", url_part)[0]

            temp_info["page_json"] = json.dumps(new_dict, ensure_ascii=False)
            di_model.lists.append(temp_info)

        result.next_dicts.insert.append(di_model)

    return result


def kejiribao_kejiribaopage_callback(callmodel: CallBackModel[PaperPageModel]) -> DealModel:
    result = DealModel()
    para_dicts = callmodel.para_dicts
    data = para_dicts["data"]
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    info_dicts = {"task_name": callmodel.sql_model.task_name,
                  "task_tag": callmodel.sql_model.task_tag,
                  "task_tag_next": task_info.task_tag_next}
    befor_json = callmodel.sql_model.dict()
    befor_json = json.loads(befor_json['page_json'])
    if "1_1" in data:
        di_model = DealInsertModel()
        di_model.insert_pre = CoreSqlValue.insert_ig_it
        for item in data["1_1"]['info']['children']:
            title = item['title']
            url_part = item['url_part']
            year = befor_json['year']
            months = befor_json['months']
            days = befor_json['days']
            page = befor_json['page']
            versions_title = befor_json['versions_title']
            versions_url = befor_json['versions_url']
            rawid_page = callmodel.sql_model.rawid_page
            index = re.findall(f'content_(.*?)\.', url_part)[0]

            temp_info = info_dicts.copy()
            task_tag_next = temp_info["task_tag_next"]
            temp_info["task_tag"] = task_tag_next
            del temp_info["task_tag_next"]
            temp_info["rawid"] = rawid_page + index.rjust(2, '0')
            new_dict = dict()
            new_dict["cid"] = rawid_page + index.rjust(2, '0')
            new_dict["year"] = year
            new_dict["months"] = months
            new_dict["days"] = days
            new_dict["versions_title"] = versions_title
            new_dict["title"] = title.replace(';', '')
            new_dict["page"] = page
            new_dict["index"] = index
            new_dict["url"] = re.findall("content_.*", url_part)[0]
            new_dict["versions_url"] = versions_url

            temp_info["article_json"] = json.dumps(new_dict, ensure_ascii=False)
            di_model.lists.append(temp_info)

        result.next_dicts.insert.append(di_model)

    return result


def kejiribao_kejiribaoarticle_callback(callmodel: CallBackModel[PaperArticleModel]) -> DealModel:
    result = DealModel()
    return result